using System.Data;
using MySqlConnector;

namespace Front._1118;

public partial class BookFrom : Form
{
    public BookFrom()
    {
        InitializeComponent();
    }

    private void BookFrom_Load(object sender, EventArgs e)
    {
        //查询所有图书
        GetAllBooks();
        //
        GetALlCategorise();
        
        
    }

    private void GetALlCategorise()
    {using var connection =new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=wangyuhao");
        connection.Open();
        
        String sql = "SELECT distinct book_category FROM book_Info;";
        //创建MySqlDataAdapter
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        //创建DataSet
        DataSet dataSet = new DataSet();
        //填充数据
        mySqlDataAdapter.Fill(dataSet, "book_category");
        if (dataSet.Tables["book_category"].Rows.Count > 0)
        {
            DataTable categoryTable = dataSet.Tables["book_category"];
            // 插入一行全部
            DataRow row = categoryTable.NewRow();
            row[0] = "全部";
    
            categoryTable.Rows.InsertAt(row,0);
            comboBox1.DataSource = categoryTable;
            comboBox1.DisplayMember = categoryTable.Columns[0].ColumnName;
            
        }
        
        
    }

    private void GetAllBooks()
    {
        using var connection =new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=wangyuhao");
        connection.Open();
        
        String sql = "SELECT * FROM book_Info;";
        //创建MySqlDataAdapter
        MySqlDataAdapter mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        //创建DataSet
        DataSet dataSet = new DataSet();
        //填充数据
        mySqlDataAdapter.Fill(dataSet, "book_Info");
        this.dataGridView1.DataSource = dataSet.Tables["book_Info"];
    }
}